//
//  KLSGSpeakController.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/11/1.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGSpeakController.h"
#import "KLSGSpeackView.h"
#import "KLSGProgramAndSpeakerModel.h"
#import "KLSGDetailProgramAndSpeakController.h"

@interface KLSGSpeakController ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation KLSGSpeakController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.titleString;
    [self addPickerView];
    [self addSetUpData];
}

- (void)addPickerView
{
    self.imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    self.imageView.alpha = 0.5;
    self.imageView.image = [UIImage imageNamed:@"q.jpg"];
    [self.view addSubview:self.imageView];
    
    self.imageView.userInteractionEnabled = YES;
    
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, (kScreenHeight - kScreenHeight / 2) / 2, kScreenWidth, kScreenHeight / 2)];
//    self.pickerView.backgroundColor = [UIColor redColor];
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    [self.view addSubview:self.pickerView];
}

#pragma mark -- pikerView的代理方法
//  必须要实现的两个方法
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return self.dataArray.count;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//    if (self.dataArray.count == 0) {
//        return 0;
//    }
    return 1;
}

//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
//{
//    return kScreenWidth;
//}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return kScreenHeight / 5;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    KLSGSpeackView *speakView = [[KLSGSpeackView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
    
    KLSGProgramAndSpeakerModel *model = self.dataArray[row];
    [speakView.coverImage sd_setImageWithURL:[NSURL URLWithString:model.middleLogo] placeholderImage:[UIImage imageNamed:@"default_anchor_avatar"]];
    speakView.iconImage.image = [UIImage imageNamed:@"listicon_fan"];
    speakView.figureLable.text = [NSString stringWithFormat:@"%ld", row + 1];
    speakView.titleLable.text = [NSString stringWithFormat:@"主播: %@", model.nickname];
    speakView.tagsLable.text = model.personDescribe;
    speakView.tracksCountLable.text = [NSString stringWithFormat:@"%@", model.followersCounts];
    
    return speakView;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    KLSGProgramAndSpeakerModel *model = self.dataArray[row];

    KLSGDetailProgramAndSpeakController *DPASVC = [[KLSGDetailProgramAndSpeakController alloc] init];
    DPASVC.uidString = [NSString stringWithFormat:@"%@", model.uid];
    [self.navigationController pushViewController:DPASVC animated:YES];
}

- (void)addSetUpData
{
    NSURLSession *session = [NSURLSession sharedSession];
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=iPhone&key=%@&pageId=1&pageSize=20", self.contentTypeString,self.keyString];
    NSURL *url = [NSURL URLWithString:string];
    NSMutableURLRequest *requset = [NSMutableURLRequest requestWithURL:url];
    __block KLSGSpeakController *mySelf = self;
    NSURLSessionDataTask *task = [session dataTaskWithRequest:requset completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data != nil) {
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            NSArray *array = [dictionary valueForKey:@"list"];
            
            for (NSDictionary *dic in array) {
                KLSGProgramAndSpeakerModel *model = [[KLSGProgramAndSpeakerModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [mySelf.dataArray addObject:model];
            }
            NSLog(@"%@", self.dataArray);
        }
        
        [mySelf performSelectorOnMainThread:@selector(reloadData:) withObject:mySelf.pickerView waitUntilDone:NO];
    }];
    [task resume];
    
}
- (void)reloadData:(UIPickerView *)pickerView
{
    [pickerView reloadAllComponents];
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
